package com.zekdot.userclient.action;

import com.alibaba.fastjson.JSONArray;
import com.zekdot.userclient.entity.UserEntity;
import com.zekdot.userclient.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Create by zekdot on 20-3-16.
 */
@Controller
@RequestMapping("/user")
public class UserAction {

    @Autowired
    private UserService userService;

    /**
     * 增加一名用户
     * @param userEntity 用户实体
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String add(UserEntity userEntity){
        return userService.addUser(userEntity,1).toString();
    }

    /**
     * 增加多名用户
     * @param data 用户名
     * @return
     */
    @RequestMapping(value = "/addList",method = RequestMethod.POST)
    @ResponseBody
    public String addList(String data){
        List<UserEntity> list = JSONArray.parseArray(data,UserEntity.class);
        return userService.addUsers(list).toString();
    }

    /**
     * 注册
     * @param userEntity 用户实体
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public String register(UserEntity userEntity) throws UnsupportedEncodingException {
        return userService.register(userEntity).toString();
    }

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(String username,String password) throws UnsupportedEncodingException {
        return userService.login(username,password).toString();
    }

    /**
     * 获取自己的信息
     * @param userId 用户名
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String get(String userId){
//        System.out.println(username);
        return userService.getUser(userId).toString();
    }

    /**
     * 获取他人的信息
     * @param username 用户名
     * @return
     */
    @RequestMapping(value = "/getOther",method = RequestMethod.GET)
    @ResponseBody
    public String getOther(String username){
//        System.out.println(username);
        return userService.getUser(username).toString();
    }

    /**
     * 更新用户状态
     * @param username 用户名
     * @param status 用户状态 0-启用 1-删除 2-禁用
     * @return
     */
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateUserStatus(String username,int status){
        return userService.updateUserState(username,status).toString();
    }

    /**
     * 获取更多用户
     * @param lastUsername 最后一条用户名
     * @param length 长度
     * @return
     */
    @RequestMapping(value = "/moreUsers",method = RequestMethod.GET)
    @ResponseBody
    public String getMoreUsers(String lastUsername,int length){
        return userService.getMoreUsers(lastUsername,length).toString();
    }

    /**
     * 修改密码
     * @param userId 用户id
     * @param oriPassword 原始密码
     * @param newPassword 新密码
     * @return
     */
    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    public String changePassword(String userId,String oriPassword,String newPassword){
        return userService.changePassword(userId,oriPassword,newPassword).toString();
    }

}
